package com.woniu.zsd.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.order.dto.CommentDto;
import com.woniu.zsd.order.dto.FindOrderCommentDto;
import com.woniu.zsd.order.entity.Comment;
import com.woniu.zsd.order.from.AddCommentFrom;
import com.woniu.zsd.order.from.ReplyCommentFrom;
import com.woniu.zsd.order.mapper.CommentMapper;
import com.woniu.zsd.order.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private CommentMapper commentMapper;


    @Override
    public void addComment(AddCommentFrom acf) {
        Comment comment = BeanUtil.toBean(acf, Comment.class);
        comment.setCreatTime(LocalDateTime.now());

        this.save(comment);
    }

    @Override
    public void replyComment(ReplyCommentFrom rcf) {
        Comment comment = BeanUtil.toBean(rcf, Comment.class);
        comment.setCreatTime(LocalDateTime.now());
        this.save(comment);
    }

    @Override
    public FindOrderCommentDto findComment() {
        List<Comment> comments = commentMapper.selectList(new QueryWrapper<>());
        ArrayList<CommentDto> commentDtos = new ArrayList<>();
        for (Comment comment : comments) {
            CommentDto dto = new CommentDto();
            dto.setContent(comment.getContent());
            dto.setOrderFraction(comment.getOrderFraction());
            dto.setReplyId(comment.getReplyId());
            if(comment.getReplyId()!=0)
            {
                dto.setNickName("商家回复");
            }else {
                dto.setNickName("用户昵称");
            }
            commentDtos.add(dto);

        }
        FindOrderCommentDto findOrderCommentDtos = new FindOrderCommentDto();
        findOrderCommentDtos.setCommentDto(commentDtos);
        return findOrderCommentDtos;
    }
}
